/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ds_Stack;

/**
 *
 * @author hcc25_000
 */
public class MyLindedStack<T> implements MyStackADT<T> {

    private int count;//记录栈中存储的元素个数
    private MyLinerNode<T> top;//指向栈顶的引用

    public MyLindedStack() {
        count = 0;
        top = null;
    }

    public void push(T element) {
        MyLinerNode<T> temp = new MyLinerNode(element);
        temp.setNext(top);
        top = temp;
        count++;
    }

    public T pop() {
        if (isEmpty()) {
            throw new MyEmptyCollectionException("Stack");
        }

        T result = top.getElement();
        top = top.getNext();
        count--;

        return result;
    }

    public T peek() {
        if (isEmpty()) {
            throw new MyEmptyCollectionException("Stack");
        }

        return top.getElement();
    }

    public boolean isEmpty() {
        return count == 0;
    }

    public int size() {
        return count;
    }

    public String toString() {
        return new String("MyLindedStack");
    }

    public void showInfo() {
        if (isEmpty()) {
            throw new MyEmptyCollectionException("Stack");
        }
        
        while(top!=null){
            MyLinerNode<T> guide=top;
            System.out.println(""+guide.getElement());
            guide=guide.getNext();
        }
    }
}
